<?php

/***
 * Class XmlElement can be used to create well-formed full-blown XML documents on the fly.
 ***/
class XmlElement {

   /**
	* Default system application to launch
	* @access Public
	* @var String
	*/
	private $element_name;


   /**
	* Default system application to launch
	* @access Public
	* @var String
	*/
	private $element_content;


   /**
	* Default system application to launch
	* @access Public
	* @var String
	*/
	private $element_attributes;


   /**
	* Default system application to launch
	* @access Public
	* @var String
	*/
	private $element_nodes;


   /**
	* Default system application to launch
	* @access Public
	* @var String
	*/
	private $element_buffer;


   /**
	* Default system application to launch
	* @access Public
	* @var String
	*/
	private $element_self_closings;


   // ! Constructor Method

   /**
	* Instantiates class and defines instance variables.
	*
	* @param Object $Reg Core object registry passed through by reference
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access public
	* @return Void
	*/
	public function __construct($element_name, $content = '', $attributes = array())
	{
		$this->element_name = $element_name;
		$this->element_content = $content;
		$this->element_attributes = $attributes;
		$this->element_nodes = array();
		$this->element_buffer = '';
		$this->element_self_closing = true;
	}


   // ! Mutator Method

   /**
	* Sets the default application.
	*
	* @param String $application Value for instance variable $this->application
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function addAttribute($attribute_name, $attribute_value)
	{
		$this->element_attributes[$attribute_name] = $attribute_value;
	}


   // ! Mutator Method

   /**
	* Sets the default application.
	*
	* @param String $application Value for instance variable $this->application
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function addContent($content)
	{
		$this->element_content = $content;

		return true;
	}


   // ! Mutator Method

   /**
	* Sets the default application.
	*
	* @param String $application Value for instance variable $this->application
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function addNode($Node)
	{
		$this->element_nodes[] = $Node;

		return true;
	}


   // ! Mutator Method

   /**
	* Sets the default application.
	*
	* @param String $application Value for instance variable $this->application
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function isSelfClosing($state = true)
	{
		return $this->element_self_closing = $state;
	}


   // ! Mutator Method

   /**
	* Sets the default application.
	*
	* @param String $application Value for instance variable $this->application
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function buildXmlOutput($indent = true, $depth = 0)
	{
		$this->element_buffer .= str_repeat("\t", $depth) . "<" . $this->element_name;
		$this->element_buffer .= $this->buildAttributeList();

		if($this->element_nodes || $this->element_content || false == $this->self_closing)
		{
			$this->element_buffer .= '>';

			if($this->element_content)
			{
				if(preg_match("/[\[\]'\"&<>]/", $this->element_content))
				{
					$this->element_buffer .= "<![CDATA[{$this->element_content}]]>";
				}
				else {
					$this->element_buffer .= $this->element_content;
				}
			}

			if($this->element_nodes)
			{
				$this->element_buffer .= "\n";

				foreach($this->element_nodes as $Child)
				{
					$this->element_buffer .= $Child->buildXmlOutput($indent, $depth + 1);
				}

				$this->element_buffer .= str_repeat("\t", $depth);
			}

			$this->element_buffer .= "</{$this->element_name}>\n";
		}
		else
		{
			$this->element_buffer .= "/>\n";
		}

		return $this->element_buffer;
	}


   // ! Mutator Method

   /**
	* Sets the default application.
	*
	* @param String $application Value for instance variable $this->application
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function returnXmlBuffer($header = false, $build = false )
	{
		if($build)
		{
			$this->buildXmlOutput();
		}

		if($header)
		{
			$this->element_buffer = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" . $this->element_buffer;
		}

		return $this->element_buffer;
	}


   // ! Mutator Method

   /**
	* Sets the default application.
	*
	* @param String $application Value for instance variable $this->application
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	private function buildAttributeList()
	{
		if(false == $this->element_attributes)
		{
			return '';
		}

		$out = array();

		foreach($this->element_attributes as $key => $val)
		{
			$out[] = "{$key}=\"{$val}\"";
		}

		return ' ' . implode(' ', $out);
	}


   // ! Mutator Method

   /**
	* Sets the default application.
	*
	* @param String $application Value for instance variable $this->application
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function __toString()
	{
		return $this->returnXmlBuffer(true, true);
	}
}

?>